package org.ace.view
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import org.ace.model.SocketModel;
	
	public class BattleHandler extends Sprite
	{
		
		private static var battle:BattleHandler;
		private var load:Loader;
		private var battleView:MovieClip;
		private var teamOne:Array=new Array();
		private var teamTwo:Array=new Array();
		
		private var cing:Array=new Array();
		
		public function BattleHandler()
		{
			super();
		}
		public static function getScene():BattleHandler{
			if(battle==null){
				battle=new BattleHandler();
			}
			return battle;
		}
		public function process(message:SocketModel):void{
			for(var i:int=0;i<5;i++){
				teamOne.push(i);
				teamTwo.push(i);
			}
			battleInit();
		}
		
		public function onLoad(e:Event):void{
			load.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loading);
			load.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoad);
			var c:Class=load.contentLoaderInfo.applicationDomain.getDefinition("org.ace.battle") as Class;
			battleView=MovieClip(new c);
			addChild(battleView);
			loadRes();
			
		}
		public function loading(e:ProgressEvent):void{
			Loading.getInstance().setIng((e.bytesLoaded/e.bytesTotal*100).toString());
		}
		public function loadRes():void{
			load=new Loader();
			load.contentLoaderInfo.addEventListener(Event.COMPLETE,loadResOver);
			load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loading);
			Loading.getInstance().setBody("正在加载角色资源");
			load.load(new URLRequest("res/battleRole.swf"));
		}
		
		public function loadResOver(e:Event):void{
			load.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loading);
			load.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadResOver);
			var c:Class=load.contentLoaderInfo.applicationDomain.getDefinition("org.ace.battleRole") as Class;
			for(var i:int=0;i<5;i++){
				var m:MovieClip=MovieClip(new c);
				m.gotoAndStop(1);
				m.role.gotoAndStop(1);
				m.x=760-i*20;
				m.y=180+i*80;
				battleView.addChild(m);
				cing.push(m);
			}
			
			for(var i:int=0;i<5;i++){
				var m:MovieClip=MovieClip(new c);
				m.gotoAndStop(2);
				m.role.gotoAndStop(1);
				m.x=230-i*20;
				m.y=120+i*80;
				battleView.addChild(m);
				cing.push(m);
			}
			MyGameDemo.battleView.addChild(battle);
			Loading.getInstance().removeLoading();
			battle.addEventListener(Event.ENTER_FRAME,onEnter);
		}
		private var index:int=0;
		private var isOn:Boolean=false;
		private var mcing:MovieClip;
		private var lastX:int;
		private var lastY:int;
		public function onEnter(e:Event):void{
			if(!isOn){
				if(cing.length>0){
					mcing=cing.shift();
					lastX=mcing.x;
					lastY=mcing.y;
					mcing.x=960/2;
					mcing.y=560/2;
					mcing.role.gotoAndStop(2);
					isOn=true;
				}else{
					battle.removeEventListener(Event.ENTER_FRAME,onEnter);
					battle.parent.removeChild(battle);
					battle.removeChild(battleView);
					battleView=null;
					GameSceneHandler.getScene().setRcV(true);
				}
			}else{
				index++;
				if(index>=40){
					index=0;
					isOn=false;
					mcing.x=lastX;
					mcing.y=lastY;
					mcing.role.gotoAndStop(1);
				}
			}
		}
		
		private function battleInit():void{
			if(battleView==null){
				load=new Loader();
				load.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoad);
				load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loading);
				Loading.getInstance().addLoading();
				Loading.getInstance().setBody("正在加载战斗背景");
				load.load(new URLRequest("res/battle.swf"));
			}
		}
		
		
	}
}